let storage = bui.storage();
loader.define(function (require, exports, module, global) {

    var pageview = {};

    pageview.init = function () {
        // console.log("========================我到第一个页面了")
        let org = storage.get("org",0)
        let token = storage.get("token",0)

        let index = 0
        let tzPage = storage.get("tzPage", 0);
        if (tzPage && tzPage == "2") {
            index = 2;
        }
        else if (tzPage && tzPage == "1") {
            index = 1;
        }
        if (org && org.qrcode) {
            router.$("#qrcode2").attr('src', org.qrcode)
        } else {
            router.$("#qrcode2").attr('src', 'images/qrcode.jpg')
        }

        // 自定义居中弹出框
        let gzDialog = bui.dialog({
            id: "#mainGzDialog",
            height: 300,
            // mask: false,
            callback: function(e) {}
        });

        let modPath = "/pages/";
        let uiTab = bui.tab({
            id: "#uiTabHome",
            position: "bottom",
            iconPosition: "top",
            index: index,
            animate: false,
            showNav: false,
            swipe:false,
            data: [{
                id: "uiTabHome0",
                icon: "",
                title: "首页",
                name: `${modPath}main/main`,
                param: {}
            }, {
                id: "uiTabHome1",
                icon: "",
                title: "商家",
                name: `${modPath}entPage/entPage`,
                param: {}
            }, {
                id: "uiTabHome2",
                icon: "",
                title: "我的",
                name: `${modPath}my/my`,
                param: {}
            }]
        })
        router.$("#home").click(function () {
            uiTab.to(0);
            storage.set("tzPage", '0')
            bui.$("#uiTabHomeNav .bui-btn").removeClass("active");
            router.$("#home").addClass("active");
        })
        router.$("#homeGz").click(function () {
            gzDialog.open()
        })
        router.$("#homeFb").click(function () {
            if (token) {
                bui.ajax({
                    url: global.api.verifyToken,
                    method: "POST",
                    headers: {Token: token}
                }).then(function(res){
                    if (res.code != '200') {
                        storage.set("dlTz", '0')
                        router.replace({ url: "pages/login/login.html", param:{tzSign: '0'}});
                    } else {
                        router.load({ url: "pages/fbZj/fbZj.html", param:{tzSign: '0'}});
                    }
                })
            } else {
                storage.set("dlTz", '0')
                router.replace({ url: "pages/login/login.html", param:{tzSign: '0'}});
            }
        })
        router.$("#homeHistory").click(function () {
            uiTab.to(1);
            storage.set("tzPage", '1')
            bui.$("#uiTabHomeNav .bui-btn").removeClass("active");
            router.$("#homeHistory").addClass("active");
        })
        router.$("#homeMy").click(function () {
            if (token) {
                bui.ajax({
                    url: global.api.verifyToken,
                    method: "POST",
                    headers: {Token: token}
                }).then(function(res){
                    if (res.code != '200') {
                        storage.set("dlTz", '2')
                        router.replace({ url: "pages/login/login.html", param:{tzSign: '0'}});
                    } else {
                        storage.set("tzPage", '2')
                        uiTab.to(2);
                        bui.$("#uiTabHomeNav .bui-btn").removeClass("active");
                        router.$("#homeMy").addClass("active");
                    }
                })
            } else {
                storage.set("dlTz", '2')
                router.replace({ url: "pages/login/login.html", param:{tzSign: '0'}});
            }
        })
    }

    // 初始化
    pageview.init();

    // 输出模块
    return pageview;
})
